package top.houry.netty.barrage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.google.protobuf.TextFormat;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.houry.netty.barrage.annotation.BarrageAnnotation;
import top.houry.netty.barrage.consts.BarrageMsgTypeConst;
import top.houry.netty.barrage.proto.BarrageProto;
import top.houry.netty.barrage.service.IBarrageMsgService;
import top.houry.netty.barrage.service.IBarrageMsgTypeService;
import top.houry.netty.barrage.service.IBarrageWatchInfoService;
import top.houry.netty.barrage.utils.BarrageConnectInfoUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/1/22
 **/
@Service
@BarrageAnnotation(msgType = BarrageMsgTypeConst.WEB_CLIENT_USER_LOGIN_REQ)
@Slf4j
public class BarrageClientUserLoginMsgServiceImpl implements IBarrageMsgTypeService {

    private static final Map<String, String> userMap = new HashMap<>();

    static {
        userMap.put("barrage", "123456");
    }

    @Override
    public void dealWithBarrageMessage(BarrageProto.Barrage barrage, ChannelHandlerContext ctx) {
        try {
            BarrageProto.WebClientUserLoginReq userLoginReq = BarrageProto.WebClientUserLoginReq.parseFrom(barrage.getBytesData());
            log.info("Req-BarrageClientUserLoginMsgServiceImpl-dealWithBarrageMessage-params:{}",  TextFormat.printToUnicodeString(userLoginReq));
            String password = userLoginReq.getPassword();
            String username = userLoginReq.getUsername();
            if (StrUtil.isBlank(password) || StrUtil.isBlank(username)) {
                log.info("BarrageClientUserLoginMsgServiceImpl-dealWithBarrageMessage-请求参数不全");
                return;
            }
            if (userMap.get(username).equals(password)) {
                // 下发token标识
            }

        } catch (Exception e) {
            log.error("Exception-BarrageClientUserLoginMsgServiceImpl-dealWithBarrageMessage", e);
        }
    }

}
